# This file is part of BlackArch Linux ( https://www.blackarch.org/ ).
# See COPYING for license details.
#
# adjusted to our needs and style
#
# Maintainer: David Runge <dvzrv@archlinux.org>
# Maintainer: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Maintainer: Lukas Fleischer <lfleischer@archlinux.org>
# Contributor: Gaetan Bisson <bisson@archlinux.org>
# Contributor: Tobias Powalowski <tpowa@archlinux.org>
# Contributor: Andreas Radke <andyrtr@archlinux.org>
# Contributor: Judd Vinet <jvinet@zeroflux.org>

pkgname=gnupg
pkgver=2.2.41
pkgrel=3
pkgdesc='Complete and free implementation of the OpenPGP standard.'
arch=('x86_64' 'aarch64')
url='https://www.gnupg.org/'
license=('BSD' 'custom' 'custom:CC0' 'GPL2' 'GPL3' 'LGPL3' 'LGPL2.1' 'MIT')
depends=('bzip2' 'libbz2.so' 'glibc' 'gnutls' 'libgcrypt' 'libgpg-error'
         'libksba' 'libassuan' 'libassuan.so' 'npth' 'libnpth.so' 'pinentry'
         'readline' 'libreadline.so' 'sqlite' 'zlib')
makedepends=('libldap' 'libusb-compat' 'pcsclite')
optdepends=(
  'libldap: gpg2keys_ldap'
  'libusb-compat: scdaemon'
  'pcsclite: scdaemon'
)
install="$pkgname.install"
source=("https://gnupg.org/ftp/gcrypt/${pkgname}/${pkgname}-${pkgver}.tar.bz2"
        'drop-import-clean.patch'
        'avoid-beta-warning.patch')
sha512sums=('f472e5058ea9881355f0c754a47acd0b5360c36e8976b8563dbc763a7cef792bf88227cc15fe5172d3e9bb9fc34d8448dd5c183949031e91a1997cc7f0f83b55'
            '32b4d994fdaa92582fccc1a3a2ea8ed08807fa9d7e0b4afca0415978cf3ffcbf4f95a044cb80a0c623cb28efdaa387b4008c1164f633ab4d1590f4138cf2d087'
            '1089b2789a74866e125a4c419a54be50c323bc44bf21078636d2d8122616474e840dece42b35d80e7ccac16a2f5685b4fa4d8a9215e908daca3fcccfbcd81909')

prepare() {
  cd "$pkgname-$pkgver"

  local src
  for src in "${source[@]}"; do
    src="${src%%::*}"
    src="${src##*/}"
    [[ $src = *.patch ]] || continue
    msg2 "Applying patch $src..."
    patch -Np1 < "../$src"
  done

  # improve reproducibility
  rm doc/gnupg.info*

  ./autogen.sh
}

build() {
  cd "$pkgname-$pkgver"

  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --sbindir=/usr/bin \
    --libexecdir=/usr/lib/gnupg \
    --enable-maintainer-mode \

  make
}

package() {
  cd "$pkgname-$pkgver"

  local units=({dirmngr,gpg-agent{,-{browser,extra,ssh}}}.socket)
  local socket_target_dir="$pkgdir/usr/lib/systemd/user/sockets.target.wants/"
  local unit

  make DESTDIR="$pkgdir" install
  ln -s gpg "${pkgdir}"/usr/bin/gpg2
  ln -s gpgv "${pkgdir}"/usr/bin/gpgv2

  install -Dm 644 doc/examples/systemd-user/*.* \
    -t "$pkgdir/usr/lib/systemd/user"
  install -Dm 644 COPYING.{CC0,other} \
    -t "$pkgdir/usr/share/licenses/$pkgname/"

  install -vdm 755 "$socket_target_dir"
  for unit in "${units[@]}"; do
    ln -sv "../$unit" "$socket_target_dir$unit"
  done
}

